﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;

namespace AugustVisual
{
    public delegate void HeaderColumnCellMouseDownHandle(HeaderColumnCell headerColumnCell, Binding dispBinding,  int sortState);
    public class HeaderColumnCell : Button
    {
        static HeaderColumnCell()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderColumnCell), new FrameworkPropertyMetadata(typeof(HeaderColumnCell)));
        }
        public Binding DispBinding  
        {
            get { return (Binding)GetValue(DispBindingProperty); }
            set { SetValue(DispBindingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DispBindingProperty =
            DependencyProperty.Register("DispBinding", typeof(Binding), typeof(HeaderColumnCell), new FrameworkPropertyMetadata(null));


        public event HeaderColumnCellMouseDownHandle HeaderColumnCellMouseDown;


        private HeaderColumnRow parentHeaderColumnRow;

        public HeaderColumnRow ParentHeaderColumnRow
        {
            get { return parentHeaderColumnRow; }
            set { parentHeaderColumnRow = value; }
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            if (parentHeaderColumnRow != null)
            {
                parentHeaderColumnRow.InvalidateMeasure();
            }
        }


        //protected override void OnInitialized(EventArgs e)
        //{
        //    //Style style = (Style)FindResource("HeaderColumnCellStyle");
        //    //this.Style = style;
        //}

        public HeaderColumnCell()
        {

        }

        private int sortState = 0;
        internal FrameworkElement upsortArrow = null;
        internal FrameworkElement downsortArrow = null;
        internal int SortState
        {
            get
            {
                return sortState;
            }
            set
            {
                sortState = value;
                if (upsortArrow == null)
                    upsortArrow = FrameworkElementHelper.Findchild(this, "upsortArrow");
                if (downsortArrow == null)
                    downsortArrow = FrameworkElementHelper.Findchild(this, "downsortArrow");
                switch (sortState)
                {
                    case 0:
                        upsortArrow.Visibility = Visibility.Hidden;
                        downsortArrow.Visibility = Visibility.Hidden;
                        break;
                    case 1:
                        upsortArrow.Visibility = Visibility.Visible;
                        downsortArrow.Visibility = Visibility.Hidden;
                        break;
                    case 2:
                        upsortArrow.Visibility = Visibility.Hidden;
                        downsortArrow.Visibility = Visibility.Visible;
                        break;
                }
               
            }
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            if (sortState == 0)
            {
                SortState = 1;
            }
            else if (sortState == 1)
            {
                SortState = 2;
            }
            else
            {
                SortState = 0;
            }
            this.parentHeaderColumnRow.SelectColumn(this);
            HeaderColumnCellMouseDown?.Invoke(this, DispBinding, sortState);
        }

        bool IsInDesignMode => (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue;

        protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
        {
            base.OnTemplateChanged(oldTemplate, newTemplate);
            _ = ApplyTemplate();
            Thumb rrr = (Thumb)GetTemplateChild("PART_RightHeaderGripper");
            if (!IsInDesignMode)
            {
                rrr.DragDelta -= Rrr_DragDelta;
                rrr.DragDelta += Rrr_DragDelta;
            }
        }

        private void Rrr_DragDelta(object sender, DragDeltaEventArgs e)
        {
            double w = Width + e.HorizontalChange;
            if (w > 10)
            {
                Width = w;
            }
        }
    }
}
